#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
extern int count;

typedef struct
{
	int mine;
	int count;
	int reveal;
	char show;
} MineField;

MineField mine[HS][LS];

void Menu()
{
	printf("***********************************\n");
	printf("********  Mine Sweeper V3  ********\n");
	printf("***********************************\n");
	printf("********      1. Play      ********\n");
	printf("********      0. Exit      ********\n");
	printf("***********************************\n");
	printf("***********************************\n");
	printf("Play?(1/0)\n");
}

void Initialize()
{
	int i = 0, j = 0;
	for (i = 0; i < HS; i++)
	{
		for (j = 0; j < LS; j++)
		{
			mine[i][j].mine = 0;
			mine[i][j].count = 0;
			mine[i][j].reveal = 0;
			mine[i][j].show = '*';
		}
	}
}

void Place()
{
	for (;count > 0;)
	{
		int a = (rand() % H) + 1;
		int b = (rand() % L) + 1;
		if (mine[a][b].mine == 0)
		{
			mine[a][b].mine = 1;
			count--;
		}
	}
}

void Count()
{
	int i = 1, j = 1;
	for (i = 1; i <= H; i++)
	{
		for (j = 1; j <= L; j++)
		{
			if (0 == mine[i][j].mine)
			{
				mine[i][j].count =
				mine[i - 1][j - 1].mine +
				mine[i - 1][j].mine +
				mine[i - 1][j + 1].mine +
				mine[i][j - 1].mine +
				mine[i][j + 1].mine +
				mine[i + 1][j - 1].mine +
				mine[i + 1][j].mine +
				mine[i + 1][j + 1].mine;
			}
		}
	}
}

void Show(int z)
{
	int i = 1, j = 1;
	
	for (i = 1; i <= H; i++)
	{
		printf("---");
	}
	printf("\n");
	printf("\n\n  0\033[A");
	for (i = 1; i <= H; i++)
	{
		printf("\033[A%d\033[B\033[D%d ", i / 10, i % 10);
	}
	printf("\033[E\033[3C");
	for (i = 1; i <= H; i++)
	{
		printf("--");
	}
	printf("\n");
	for (i = 1; i <= H; i++)
	{
		printf("%.2d|", i);
		for (j = 1; j <= L; j++)
		{
			if (0 == z)
			{
				printf("%c ", mine[i][j].show);
			}
			else if (1 == z)
			{
				if (1 == mine[i][j].mine)
				{
					mine[i][j].show = '!';
				}
				else
				{
					mine[i][j].show = ' ';
				}
				printf("%c ", mine[i][j].show);
			}
		}
		printf("\n");
	}
	for (i = 1; i <= H; i++)
	{
		printf("---");
	}
	printf("\n");
}

int Reveal()
{

	while (1)
	{
		int choose = 0;
		printf("Reveal/Flag/Deflag(1/2/3)\n");
		scanf("%d", &choose);
		switch (choose)
		{
		case 1:
		{
			int x, y;
			printf("Enter the coordinates.\n");
			scanf("%d %d", &x, &y);
			if (x < 1 || y < 1 || x > H || y > L)
			{
				printf("Typing error.\n");
				continue;
			}
			else if (1 == mine[x][y].reveal)
			{
				printf("That slot was revealed.\n");
				continue;
			}
			else if (1 == mine[x][y].mine)
			{
				printf("You have lost.\n");
				Show(1);
				return 0;
			}
			else
			{
				Spread(x, y);
				//mine[x][y].show = mine[x][y].count + 48;
				return 1;
			}
			break;
		}
		case 2:
		{
			int x, y;
			printf("Enter the coordinates.\n");
			scanf("%d %d", &x, &y);
			if (x < 1 || y < 1 || x > H || y > L)
			{
				printf("Typing error.\n");
				continue;
			}
			else if (1 == mine[x][y].reveal)
			{
				printf("That slot is revealed.\n");
			}
			else
			{
				mine[x][y].show = '!';
				return 1;
			}
			break;
		}
		case 3:
		{
			int x, y;
			printf("Enter the coordinates.\n");
			scanf("%d %d", &x, &y);
			if (x < 1 || y < 1 || x > H || y > L)
			{
				printf("Typing error.\n");
				continue;
			}
			else if ('!' != mine[x][y].show)
			{
				printf("That slot is not a flag.\n");
			}
			else
			{
				mine[x][y].show = '*';
				return 1;
			}
			break;
		}
		}
	}
}

void Spread(int x, int y)
{






}


int Game()
{
	int reveal = H * L - count;
	Initialize();
	Place();
	Count();
	while (1)
	{
		Show(0);
		if (0 == Reveal())
		{
			return 0;
		}
		else
		{
			reveal--;
		}
		if (0 == reveal)
		{
			printf("You win!\n");
			Show(1);
			return 1;
		}
	}
}




//void Spread(int x, int y)
//{
//	int a = x - 1, b = y - 1;
//	//if (0 == mine[x][y].count && 0 == mine[x][y].reveal)
//	//{
//		//if (1 == x || 1 == y || H == x || L == y)
//		//{
//		//	mine[x][y].show = ' ';
//		//	mine[x][y].reveal = 1;
//		//}
//	if (1 < a && H >= a && 1 < b && L >= b && 0 == mine[a][b].reveal)
//	{
//		for (a = x - 1; a <= x + 1; a++)
//		{
//			for (b = y - 1; b <= y + 1; b++)
//			{
//				if (0 == mine[a][b].count)
//				{
//					mine[a][b].show = ' ';
//				}
//				else
//				{
//					mine[a][b].show = mine[a][b].count;
//				}
//				mine[a][b].reveal = 1;
//				Spread(a, b);
//			}
//		}
//	}
//	else if (0 == mine[x][y].count)
//	{
//		mine[x][y].show = ' ';
//		mine[x][y].reveal = 1;
//	}
//	else if (0 == mine[x][y].reveal)
//	{
//		mine[x][y].show = mine[x][y].count + 48;
//		mine[x][y].reveal = 1;
//	}
//}
